package mango.web.client.admin;

import java.util.Map;

import mango.web.client.C;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.googlecode.gchart.client.GChart;

public class FlowerChart extends GChart {

	public String[] colorArray = { "#FF0000", "#00FF00", "#0000FF", "#008080", "#800000", "#800080", "#808000", "#00FFFF", "#000080", "#FF00FF", "#FFFF00" };

	public void update(String type) {
		this.clearCurves();
		this.setChartSize(680, 300);

		DateTimeFormat dtf = DateTimeFormat.getFormat("dd-MMM-yyyy");
		setChartTitle("<b><i><big>Crop Forecast</big></i></b>");
		//setPadding("5px");
		//setLegendLocation(LegendLocation.INSIDE_TOPLEFT);

		getXAxis().setAxisLabel("<small><b><i>Time</i></b></small>");
		getXAxis().setTickCount(10);
		getXAxis().setHasGridlines(true);
		getXAxis().setTickLabelFormat("=(Date)dd-MMM-yy");

		getYAxis().setAxisLabel("<small><b><i>Percent</i></b></small>");
		getYAxis().setHasGridlines(true);
		getYAxis().setTickCount(11);
		getYAxis().setAxisMin(0);
		getYAxis().setAxisMax(100);

		Map<String, FlowerChartVo> flowerChartMap = C._flowerChartList.get(type);

		/*
		Date min = null;
		Date max = null;
		for (int i = 0; i < flowerChartList.size(); i++) {
			FlowerChartVo flowerChartVo = flowerChartList.get(i);
			if (flowerChartVo == null) continue;

			if (min == null || flowerChartVo.xyList.get(0).x.before(min)) min = flowerChartVo.xyList.get(0).x;
			if (max == null || flowerChartVo.xyList.get(flowerChartVo.xyList.size() - 1).x.after(max)) max = flowerChartVo.xyList.get(flowerChartVo.xyList.size() - 1).x;
		}
		getXAxis().setAxisMin(min.getTime());
		getXAxis().setAxisMax(max.getTime());
		*/

		int k = 0;
		for (String key : flowerChartMap.keySet()) {
			FlowerChartVo flowerChartVo = flowerChartMap.get(key);
			k++;
			int colorIndex = (k) % colorArray.length;

			String str = flowerChartVo.name + " [" + flowerChartVo.tree + "]";

			addCurve();
			getCurve().setLegendLabel("<b>" + str + "</b>");
			getCurve().getSymbol().setBorderColor(this.colorArray[colorIndex]);
			getCurve().getSymbol().setBackgroundColor(this.colorArray[colorIndex]);
			getCurve().getSymbol().setHeight(10);
			getCurve().getSymbol().setWidth(10);
			getCurve().getSymbol().setBorderWidth(1);
			getCurve().getSymbol().setSymbolType(SymbolType.LINE);
			getCurve().getSymbol().setFillThickness(2);
			getCurve().getSymbol().setFillSpacing(0);

			for (int j = 0; j < flowerChartVo.xyList.size(); j++) {
				XYDateDoubleVo xy = flowerChartVo.xyList.get(j);
				getCurve().addPoint(xy.x.getTime(), xy.y);
			}
			//break;
		}

		this.update();
	}
}
